using GameFramework.Battle.Core;
using Yoozoo.Framework.Managers;
using Yoozoo.Gta.Common;
using Yoozoo.Managers;

namespace Gameplay.PVE.TurnBase
{
    public class TurnController
    {
        private enum TurnType
        {
            MyTurn,
            Transition,
            EnemyTurn,
        }

        private TurnType currentTurnType;
        private int currentRound = 0;

        private int currentHit;
        private int CurrentHit
        {
            get { return currentHit; }
            set
            {
                currentHit = value; 
                ResidentHandler.Inst.GetFunction("pveManager.TbsUpdateHitCount").Action(currentHit);
            }
        }

        private bool isStart = false;
        
        public void Init()
        {
            isStart = true;
            NewRound(true);
        }

        public void Update()
        {
            if (!isStart)
            {
                return;
            }
            if (currentTurnType == TurnType.MyTurn)
            {
                var myList = TurnBaseManager.Instance.GetAllUnitByTeam(0);
                for (int i = 0; i < myList.Count; i++)
                {
                    var unit = myList[i];
                    if (unit.Data.IsCanMove() || unit.Data.IsInAction)
                    {
                        return;
                    }
                }
                //开始轮到对方回合
                currentTurnType = TurnType.Transition;
                TimeManager.Instance.DelayCall(400, () =>
                {
                    currentTurnType = TurnType.EnemyTurn;
                    CurrentHit = 0;
                });
            }
            else if (currentTurnType == TurnType.EnemyTurn)
            {
                var list = TurnBaseManager.Instance.GetAllUnitByTeam(2);
                bool isUnitInAction = false;
                for (int i = 0; i < list.Count; i++)
                {
                    var unit = list[i];
                    if (unit.Data.IsEnemyCastCd)
                    {
                        isUnitInAction = true;
                        break;
                    }
                }

                if (!isUnitInAction)
                {
                    bool isCanNextRound = true;
                    for (int i = 0; i < list.Count; i++)
                    {
                        var unit = list[i];
                        if (unit.Data.IsCanMove())
                        {
                            unit.Transmit(ETransmitType.BeforeRound);
                            if (!unit.Data.IsCanMove())
                            {
                                continue;
                            }
                            if (unit.Data.CanUseSkill())
                            {
                                unit.Transmit(ETransmitType.Skill);
                            }
                            else
                            {
                                unit.Transmit(ETransmitType.Attack);
                            }
                            unit.Data.RestMoveCount--;
                            isCanNextRound = false;
                            break;
                        }
                        else if (unit.Data.IsInAction)
                        {
                            isCanNextRound = false;
                        }
                    }

                    if (isCanNextRound)
                    {
                        NewRound();
                    }
                }
            }
        }

        private void NewRound(bool isFirst = false)
        {
            currentTurnType = TurnType.MyTurn;
            var list = TurnBaseManager.Instance.GetAllUnitByTeam(0);
            for (int i = 0; i < list.Count; i++)
            {
                list[i].Data.RestMoveCount = 1;
                if (!isFirst)
                {
                    list[i].Transmit(ETransmitType.RoundEnd);
                }
                
            }
            list = TurnBaseManager.Instance.GetAllUnitByTeam(2);
            for (int i = 0; i < list.Count; i++)
            {
                list[i].Data.RestMoveCount = 1;
                if (!isFirst)
                {
                    list[i].Transmit(ETransmitType.RoundEnd);
                }
            }
            currentRound++;
            //处理回合开始
            list = TurnBaseManager.Instance.GetAllUnitByTeam(0);
            for (int i = 0; i < list.Count; i++)
            {
                if (isFirst)
                {
                    list[i].Transmit(ETransmitType.StageStart);
                }
                list[i].Transmit(ETransmitType.RoundStart);
            }
            list = TurnBaseManager.Instance.GetAllUnitByTeam(2);
            for (int i = 0; i < list.Count; i++)
            {
                if (isFirst)
                {
                    list[i].Transmit(ETransmitType.StageStart);
                }
                list[i].Transmit(ETransmitType.RoundStart);
            }
        }

        public void AddHit()
        {
            CurrentHit++;
        }

        public int GetRound()
        {
            return currentRound;
        }
    }
}